﻿#region sentencias using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.GamerServices;
#endregion

/**clase creada por el JC*/
namespace HS {
    class TexturaAnimada {
        private int framecount;
        private Texture2D myTexture;
        private float timePerFrame;
        private int frame;
        private float totalElapsed;

        public Vector2 position;

        public TexturaAnimada(Vector2 pos, int frameCount, int framesPerSec) {
            position = pos;
            framecount = frameCount;
            timePerFrame = 1.0f / framesPerSec;
            frame = 0;
            totalElapsed = 0;
        }

        public void Load(ContentManager content, string textName) {
            myTexture = content.Load<Texture2D>(textName);
        }

        public void UpdateFrame(float elapsed) {
            totalElapsed += elapsed;
            if (totalElapsed > timePerFrame) {
                frame++;
                //Los frames son circulares, al llegar al último volvemos al primero.
                frame = frame % framecount;
                totalElapsed -= timePerFrame;
            }
        }

        public void DrawFrame(SpriteBatch batch) {
            DrawFrame(batch, frame);
        }

        public void DrawFrame(SpriteBatch batch, int frame) {
            int FrameWidth = myTexture.Width / framecount;

            Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0,
                FrameWidth, myTexture.Height);

            batch.Draw(myTexture, position, sourcerect, Color.White,
                0, new Vector2(0, 0), 1, SpriteEffects.None, 1);
        }


    }
}
